package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.adapter.recycler.RecyclerBaseAdapter;
import com.tianze.library.base.BaseStoreHouseRefreshRecyclerFragment;
import com.tianze.library.view.EmptyLayout;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.BrowseMGJTaskOrderInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;
import com.tiza.xiaoyoubao.utils.DateUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by mapple on 17/4/10.
 * 手机蘑菇街任务(浏览赚金)
 */

public class BrowseMpMGJTaskFragment extends BaseStoreHouseRefreshRecyclerFragment {

  private List<BrowseMGJTaskOrderInfo> infoList;
        @Override
        protected RecyclerBaseAdapter getListAdapter() {
            return new TaskListAdapter();
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            pageSize = Integer.MAX_VALUE;
        }

        @Override
        protected void onItemClick(View view, int position) {
            super.onItemClick(view, position);
            BrowseMGJTaskOrderInfo info = (BrowseMGJTaskOrderInfo) mAdapter.getData().get(position);
            Hawk.put(Constants.KEY_BROWSEMPMGJTASKOEDER_INFO,info);
                Bundle args = new Bundle();
                args.putSerializable("info", info);
            UIHelper.showBrowseMpMGJTaskDetial(mActivity,args);
        }

        public static class TaskListAdapter extends RecyclerBaseAdapter {

            @Override
            protected View onCreateItemView(ViewGroup parent, int viewType) {
                return getLayoutInflater(parent.getContext()).inflate(R.layout
                                                                              .item_browse_mp_mgj_task, parent, false
                );
            }

            @Override
            protected ViewHolder onCreateItemViewHolder(View view, int viewType) {
                return new ViewHolder(viewType, view);
            }

            @Override
            public void onBindItemViewHolder(RecyclerBaseAdapter.ViewHolder holder, int position) {
                super.onBindItemViewHolder(holder, position);
                final ViewHolder vh = (ViewHolder) holder;
                BrowseMGJTaskOrderInfo info = (BrowseMGJTaskOrderInfo) _data.get(position);
                vh.tv_money.setText(DateUtils.formateRate(info.getBaseCmi()));
                vh.tv_time.setText("2017-03-23 13:11左右");
                vh.tv_type.setText(info.getBackName());
                vh.tv_goodsType.setText(info.getTypeName());
                if (TextUtils.isEmpty(info.getTastNumber())) {
                    vh.view1.setVisibility(View.GONE);
                    vh.view.setVisibility(View.VISIBLE);
                    vh.tv_taskId1.setText("ID:" + info.getUserId());
                    vh.tv_taskMoney1.setText(info.getPrice() + "元");
                } else {
                    vh.view.setVisibility(View.GONE);
                    vh.view1.setVisibility(View.VISIBLE);
                    vh.tv_taskNum.setText("剩余" + info.getTastNumber() + "单");
                    vh.tv_taskId.setText("ID:" + info.getUserId());
                    vh.tv_taskMoney.setText(info.getPrice() + "元");
                }
            }

            static class ViewHolder extends RecyclerBaseAdapter.ViewHolder {

                @BindView(R.id.view)
                View view;
                @BindView(R.id.view1)
                View view1;
                @BindView(R.id.tv_taskId)
                TextView tv_taskId;
                @BindView(R.id.tv_taskId1)
                TextView tv_taskId1;
                @BindView(R.id.tv_taskMoney)
                TextView tv_taskMoney;
                @BindView(R.id.tv_taskMoney1)
                TextView tv_taskMoney1;
                @BindView(R.id.tv_money)
                TextView tv_money;
                @BindView(R.id.tv_time)
                TextView tv_time;
                @BindView(R.id.tv_type)
                TextView tv_type;
                @BindView(R.id.iv_money)
                ImageView iv_money;
                @BindView(R.id.tv_taskNum)
                TextView tv_taskNum;
                @BindView(R.id.tv_goodsType)
                TextView tv_goodsType;

                ViewHolder(int viewType, View view) {
                    super(viewType, view);
                    ButterKnife.bind(this, view);
                }
            }
        }

            @Override
            protected void requestData(boolean refresh) {
                super.requestData(refresh);
                ApiHttpClient.getTaskInfo(this, "8",
                                            new JsonCallback<BrowseMGJTaskOrderInfo>(BrowseMGJTaskOrderInfo
                                                                                       .class) {

                                                @Override
                                                public void onError(Call call, Exception e) {
                                                    onRequestError();
                                                }

                                                @Override
                                                public void onResponse(ResponseInfo<BrowseMGJTaskOrderInfo> response) {
                                                    int code = response.getCode();
                                                    if (code == 1) {
                                                        mErrorLayout
                                                                .setErrorType(EmptyLayout.NETWORK_ERROR);
                                                        return;
                                                    }
                                                    infoList = response.getData();
                                                    onRequestResponse(infoList);
                                                }

                                                @Override
                                                public void onAfter() {
                                                    super.onAfter();
                                                    onRequestFinish();
                                                }
                                            }
                );
            }
}
